<?php

class GoodsAdvertising extends BaseModel
{
    protected $res;
    protected $redis_key = "admin_";

    public function getSource()
    {
        return "f_goods_advertising";
    }

    public function initialize()
    {
        parent::initialize();
    }

    /**
     * @func 获取详情
     * @param $id
     * @param $switch
     * @return Mixed
     */
    public function find_by_id($id, $switch = false)
    {
        if (empty($id)) {
            return [];
        }
        $info = $this->findFirstById($id);
        if ($switch) {
            return $info;
        }
        if ($info) {
            return $info->toarray();
        } else {
            return [];
        }
    }

    public function goods_advertising_list($params = [])
    {
        $page = intval($params['page']);
        $size = intval($params['limit']);
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $condition = '1=1 ';
        if (isset($params['keywords']) && !empty($params['keywords'])) {
            $condition .= " and name like '%{$params['keywords']}%'";
        }
        $cate_list = $this::find(
            array(
                $condition,
                "order"  => 'id desc',
                //                "columns" => 'id,cid,item_id,title,short_title,shop_type,pic_logo,coupon_money,price,end_price,month_sale,site,created_at,updated_at,top,status',
                'limit'  => $size,
                'offset' => $page
            ));
//        //直接获取最后一条sql语句
//        echo $this->di->get('profiler')->getLastProfile()->getSQLStatement();
//        exit;

        return $cate_list;
    }

    public function goods_advertising_count($params = [])
    {

        $condition = '1=1 ';
        if (isset($params['keywords']) && !empty($params['keywords'])) {
            $condition .= " and name like '%{$params['keywords']}%'";
        }
        $cate_list = $this::count(
            array(
                $condition,
                "columns" => 'id',
            ));

        return $cate_list;
    }

    public function goods_advertising_details($params)
    {
        $data = $this::findFirst($params);
        return $data;
    }

    public function goods_advertising_all()
    {
        return $this::find();
    }

    public function save_params($params)
    {
        $info['name']          = $params['name'];
        $info['path']          = $params['path'];
        $info['app_path']      = $params['app_path'];
        $info['type']          = $params['type'];
        $info['url']           = $params['url'];
        $info['app_data']      = $params['app_data'];
        $info['is_tb_atuh']    = $params['is_tb_atuh'];
        $info['is_need_login'] = $params['is_need_login'];
        $info['notice_id']     = $params['notice_id'];
        $info['start_color']   = $params['start_color'];
        $info['end_color']     = $params['end_color'];
        $info['relationId']    = $params['relationId'];
        $result                = $this::save($info);
        return $result;
    }

}
